/*
 * RegisterFile class holds the four General Purpose Progammable Registers and the
 * Register Select Registers.
 * 
 * Component register are Registers 0, Registers 1, Registers 2, Registers 3, 
 * Register Select Register 0 and Register Select Register 1.
 * 
 * The register select registers are used to determine which register is being
 * referenced through instructions allowing the dynamic selection of registers
 * for the rest of the system.
 * */

package cpu.components;

import cpu.datastores.*;
import cpu.datatypes.*;
import cpu.interfaces.EventListener;
import cpu.messages.Event;

public class RegisterFile {
	// General Purpose Register 0
	public Register16 R0;
	
	// General Purpose Register 1
	public Register16 R1;
	
	// General Purpose Register 2
	public Register16 R2;
	
	// General Purpose Register 3
	public Register16 R3;
	
	// Register Select Register 0
	public Register2 RSR0;
	
	// Register Select Register 1
	public Register2 RSR1;
	
	private cpu.components.CPU cpu;
	
	public RegisterFile( cpu.components.CPU cpu ) {
		this.cpu = cpu;
		
		R0 = new Register16( );
		R1 = new Register16( );
		R2 = new Register16( );
		R3 = new Register16( );
		
		RSR0 = new Register2( );
		RSR1 = new Register2( );
	}
	
	public Register16 get( Register2 rsr ) {
		int r = rsr.toJavaInt( );
		
		switch( r ) {
		case 3:
			return R3;
		case 2:
			return R2;
		case 1:
			return R1;
		case 0:
		default:
			return R0;
		}
	}
	public Register16 get( int rsr ) {
		if( rsr == 0 )
			return get( RSR0 );
		return get( RSR1 );
	}
	public void set( Register2 rsr, Word word ) {
		int r = rsr.toJavaInt( );
		
		switch( r ) {
		case 3:
			R3.fromWord( word );
			break;
		case 2:
			R2.fromWord( word );
			break;
		case 1:
			R1.fromWord( word );
			break;
		case 0:
			R0.fromWord( word );
			break;
		}
	}
	public void set( int rsr, Word word ) {
		if( rsr == 0 )
			set( RSR0, word );
		else
			set( RSR1, word );
	}
	public void setDirect( int r, Word word ) {
		switch( r ) {
		case 3:
			R3.fromWord( word );
			break;
		case 2:
			R2.fromWord( word );
			break;
		case 1:
			R1.fromWord( word );
			break;
		case 0:
			R0.fromWord( word );
			break;
		}
	}
}
